/*
 * @Author: Dorange.Wang
 * @Date: 2021-03-31 16:03:05
 * @LastEditors: wangming
 * @LastEditTime: 2022-08-03 14:49:51
 * @Description: file content
 */
import Vue from 'vue';
import template from './template';
import store from '@/store'
import router from '@/router';
import i18n from '@/i18n'
router.afterEach(async () => {
  CreateInstance.clearVm()
});
const ModalTemplate = {
  store,
  router,
  i18n,
  methods: {
    async showModel(...arg) {
      this.$refs._component.showModel(...arg)
    },
    closeModel(...arg) {
      this.$refs._component.closeModel(...arg)
    }
  },
  render(h) {
    return h(template, {
      ref: '_component',
      on: {
        onclose(key) {
          let vm = CreateInstance.instance[key]
          //  预览设计模式防止模态框配置信息缓存
          if (process.env.VUE_APP_USER === "ADMIN" && !!vm) {
            vm.$el.remove()
            vm.$destroy()
            CreateInstance.instance[key] = null
          }
        }
      }
    })
  }
}
class CreateInstance {
  static instance = {};
  static clearVm() {
    Object.values(this.instance).forEach(vm => {
      if (vm) {
        vm.$el.remove()
        vm.$destroy()
      }
    })
    this.instance = {}
  }
  static create(key) {
    if (!!this.instance[key]) {
      return this.instance[key]
    } else {
      let dialogDiv = document.createElement('div')
      document.body.appendChild(dialogDiv)
      const instance = new Vue(ModalTemplate).$mount(dialogDiv)
      this.instance[key] = instance
      return instance
    }
  }

}
export default CreateInstance;
